# Copyright (C) Kumo inc. and its affiliates.
# Author: Jeff.li lijippy@163.com
# All rights reserved.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https:#www.gnu.org/licenses/>.
#

find_package(Python REQUIRED COMPONENTS Interpreter)

execute_process(
  COMMAND
    "${Python_EXECUTABLE}" -c "\
import pyarrow
print(pyarrow.get_include())
"
  OUTPUT_VARIABLE _pyarrow_include_dir
  OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process(
  COMMAND
    "${Python_EXECUTABLE}" -c "\
import pyarrow
pyarrow.create_library_symlinks()
print(pyarrow.get_library_dirs()[0])
"
  OUTPUT_VARIABLE _pyarrow_lib_dir
  OUTPUT_STRIP_TRAILING_WHITESPACE)
message(STATUS ${_pyarrow_lib_dir})

find_library(
  _libarrow_py NAMES arrow_python
  PATHS ${_pyarrow_lib_dir}
  NO_DEFAULT_PATH)

set(pyarrow_LIBARROW_PYTHON ${_libarrow_py})
set(pyarrow_INCLUDE_DIR ${_pyarrow_include_dir})

mark_as_advanced(_libarrow_py _pyarrow_include_dir _pyarrow_lib_dir)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(
  pyarrow REQUIRED_VARS pyarrow_LIBARROW_PYTHON
                        pyarrow_INCLUDE_DIR)

if(pyarrow_FOUND)
  if(NOT TARGET pyarrow::dev)
    add_library(pyarrow::dev SHARED IMPORTED)
    set_target_properties(
      pyarrow::dev
      PROPERTIES
        IMPORTED_LINK_INTERFACE_LANGUAGES "CXX"
        IMPORTED_LOCATION "${pyarrow_LIBARROW_PYTHON}"
        INTERFACE_INCLUDE_DIRECTORIES "${pyarrow_INCLUDE_DIR}")

  endif()
endif()
